﻿

var AjaxRequestTool = Class(object, {
    Create: function () {
        //  this.ServiceURL = preUrl;//服务请求地址前部分
        //this.ServiceURL = 'http://pzh.lmego.com:8013/Server/'; //"http://admin.ls.lmego.com/Server/";//服务请求地址前部分
        this.ServiceURL = '/AdminServer/';
    },

    //get请求

    /*
    参数说明
    templete：url模板
    data：请求数据
    successCallback：访问成功时方法
    errorCallback：访问失败时候方法
    JqCoverObj：遮盖层Jq对象
    isAsync：是否为异步处理请求
    
    */
    RequestByGet: function (templete, data, successCallback, errorCallback, isAsync, maskId) {
        if (typeof (isAsync) == "undefined" || isAsync == null || isAsync=="")
            isAsync = true;

        var requestUrl = "";
        if (templete.toUpperCase().indexOf('HTTP:') == -1)
            requestUrl = this.ServiceURL + templete;
        else
            requestUrl = templete;
        if (checkMaskId(maskId)) {
            showMaskWithId(maskId);
        }

        $.ajax({
            type: "GET",
            contentType: "application/json",
            async: isAsync,
            url: requestUrl,

            dataType: "jsonp",
            beforeSend: function (x) { //发送之前动作··比如添加头文件
                x.setRequestHeader("LoginUserID", getCookie("LoginUserID"));
                x.setRequestHeader("LoginUserName", getCookie("LoginUserName"));
                x.setRequestHeader("Token", getCookie("Token"));


                x.setRequestHeader("LoginUserID", getCookie("LoginUserID"));
                x.setRequestHeader("LoginUserName", getCookie("LoginUserName"));
                x.setRequestHeader("Token", getCookie("Token"));
                x.setRequestHeader("RightCode", getCookie("RightCode"));
            },
            success: function (data, textStatus) {
                if (checkMaskId(maskId)) {
                    removeMaskWithId(maskId);
                }


                if (textStatus == "success") {
                    if (data.Message == "非法访问") {
                        parent.location.href = "/NoRight.html?autoLogin=true&info=非法访问";
                    }

                    if (!data.IsError)
                        successCallback(data.Data);
                    else {

                        if (typeof (errorCallback) != "undefined" && errorCallback)
                            errorCallback(data.Message);
                        else
                            ShowAlert(data.Message);
                    }
                } else {
                    if (typeof (errorCallback) != "undefined" && errorCallback)
                        errorCallback(data.Message);
                    else
                        ShowAlert(data.Message);
                }


            },
            error: function (xhr, textStatus, errorThrown) {
                if (checkMaskId(maskId)) {
                    removeMaskWithId(maskId);
                }
                if (xhr.status === 200 && xhr.readyState === 4) {
                    var obj = JSON.parse(xhr.responseText);
                    if(obj.Message==="非法访问")
                        parent.location.href = "/NoRight.html?autoLogin=true&info=非法访问";
                }

                etimes = new Date().getTime();


                if (typeof (errorCallback) != "undefined" && errorCallback) {
                    errorCallback(xhr, textStatus, errorThrown);

                } else {
                    //                    ShowAlert(GlobalTextInfo.Error_OpFailed);
                    //  ShowAlert(GlobalTextInfo.Error_Ser_Busy);
                    //alert(xhr.responseText);
                    //Boxy.alert("服务请求错误,请联系管理员<br/>请求地址:"+url, null, { title: "提示" });
                }


            }
        });
    },


    /*
    参数说明
    
    data：请求数据
    successCallback：访问成功时方法
    errorCallback：访问失败时候方法
    JqCoverObj：遮盖层Jq对象
    isAsync：是否为异步处理请求
    
    */

    RequestByPost: function (method, data, successCallback, errorCallback, JqCoverObj, isAsync) {
        var layerIndex = 0;
        if (typeof (isAsync) == "undefined" || isAsync == null)
            isAsync = true;
        var url = method;
        if (method.toUpperCase().indexOf('HTTP:') == -1)
            url = this.ServiceURL + url;
     
        var json = JSON.stringify(data);
        if (checkMaskId(JqCoverObj)) {
            layerIndex = layer.load(1, {
                shade: [0.2, '#000']
            });
        }
        

        //请求时间

        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: url,
            async: isAsync,
            data: json,
            dataType: "json",
           
            beforeSend: function (x) {
             
                x.setRequestHeader("LoginUserID", getCookie("LoginUserID"));
                x.setRequestHeader("LoginUserName", getCookie("LoginUserName"));
                x.setRequestHeader("Token", getCookie("Token"));
                  x.setRequestHeader("RightCode", getCookie("RightCode"));
            },
            success: function (data, textStatus) {
                if (checkMaskId(JqCoverObj)) {
                    layer.close(layerIndex);
                }
                if (textStatus == "success") {
                    if (data.Message == "非法访问") {
                        parent.location.href = "/NoRight.html?autoLogin=true&info=非法访问";
                    }

                    if (!data.IsError)
                        successCallback(data.Data);
                    else {

                        if (typeof (errorCallback) != "undefined" && errorCallback)
                            errorCallback(data.Message);
                        else
                            ShowAlert(data.Message);
                    }
                } else {
                    if (typeof (errorCallback) != "undefined" && errorCallback)
                        errorCallback(data.Message);
                    else
                        ShowAlert(data.Message);
                }




            },
            error: function (xhr, textStatus, errorThrown) {

                if (checkMaskId(JqCoverObj)) {
                    layer.close(layerIndex);
                }

                if (xhr.status === 200 && xhr.readyState === 4) {
                    var obj = JSON.parse(xhr.responseText);
                    if (obj.Message === "非法访问")
                        parent.location.href = "/NoRight.html?autoLogin=true&info=非法访问";
                }

                etimes = new Date().getTime();

                if (typeof (errorCallback) != "undefined" && errorCallback) {
                    if (errorCallback)
                        errorCallback(xhr);


                }


            }
        });
    }
});



